package com.djhu.freepartner.web.service.impl;

import com.djhu.freepartner.bean.PartnerConsumeRecord;
import com.djhu.freepartner.bean.PartnerUser;
import com.djhu.freepartner.core.Status;
import com.djhu.freepartner.core.constant.ConsumeType;
import com.djhu.freepartner.core.exception.ServiceException;
import com.djhu.freepartner.service.IPartnerConsumeRecordService;
import com.djhu.freepartner.service.IPartnerUserService;
import com.djhu.freepartner.utils.DateUtils;
import com.djhu.freepartner.web.service.BonuspointsService;
import com.djhu.freepartner.web.support.HttpKit;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BonuspointsServiceImpl implements BonuspointsService{
    @Autowired
    private IPartnerUserService userService;

    @Autowired
    private IPartnerConsumeRecordService consumeRecordService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int recharge(String userId, int account,String remark) throws ServiceException {
       PartnerUser user =  userService.selectById(userId);
       if (user == null){
           throw new ServiceException(Status.BAD_PARAM);
       }
        PartnerConsumeRecord consumeRecord = getPartnerConsumeRecord(userId);
        consumeRecord.setConsumesource(String.valueOf(ConsumeType.RECHARGE.getCode()));
        consumeRecord.setConsumesum(String.valueOf(account));
        if (StringUtils.isEmpty(remark)) {
            consumeRecord.setRemark("用户充值");
        }else {
            consumeRecord.setRemark(remark);
        }
        consumeRecord.setConsumesum(String.valueOf(account));
        consumeRecordService.insert(consumeRecord);
        int newaccount = 0;
        if (user.getAccount() == null){
            newaccount = account;
        }else {
            newaccount = user.getAccount() + account;
        }
        user.setAccount(newaccount);
        userService.updateById(user);
        return newaccount;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int consume(String userId, int account,String remark) throws ServiceException{
        PartnerUser user =  userService.selectById(userId);
        if (user == null){
            throw new ServiceException(Status.BAD_PARAM);
        }
        if (user.getAccount() < account){
            throw new ServiceException(Status.ACCOUNT_NOT_FOUNDS);
        }

        PartnerConsumeRecord consumeRecord = getPartnerConsumeRecord(userId);
        consumeRecord.setConsumesource(String.valueOf(ConsumeType.CONSUME.getCode()));
        consumeRecord.setConsumesum(String.valueOf(account));
        if (StringUtils.isEmpty(remark)) {
            consumeRecord.setRemark("用户消费");
        }else {
            consumeRecord.setRemark(remark);
        }
        consumeRecordService.insert(consumeRecord);
        int newaccount = user.getAccount() - account;
        user.setAccount(newaccount);
        userService.updateById(user);
        return newaccount;
    }

    private PartnerConsumeRecord getPartnerConsumeRecord(String userId) {
        PartnerConsumeRecord consumeRecord = new PartnerConsumeRecord();
        consumeRecord.setUserid(userId);
        consumeRecord.setUseragent(HttpKit.getRequest().getHeader("User-Agent"));
        consumeRecord.setConsumedate(DateUtils.now());
        consumeRecord.setIp(HttpKit.getIp());
        return consumeRecord;
    }
}
